#include "imagepart.h"

#include <QGraphicsSceneMouseEvent>
#include <QPainter>

ImagePart::ImagePart(const QRect &r, const int role, const int order, QGraphicsItem *parent) : QGraphicsWidget (parent), m_isValid (false)
{
        setMinimumSize (3 * borderSize (), 3 * borderSize ());
        setAcceptedMouseButtons (Qt::LeftButton);
        setFlags (ItemIsMovable | ItemIsSelectable | ItemSendsGeometryChanges);
        setRoleMax (Red);
        clear ();
        setRole (role);
        setOrder (order);

        setWindowFrameMargins (borderSize (),
                               borderSize (),
                               borderSize (),
                               borderSize ());
        setRectangle (r);
}
//---------------------------
QColor ImagePart::ipColor ()
{
    switch (role ())
    {
    case 0:
        return Qt::green;
        break;
    case 1:
        return Qt::red;
        break;
    default:
        return Qt::blue;
        break;
    }
}

//----------------------------------------
QRectF ImagePart::boundingRectToScene ()
{
    return QRectF (mapToScene (boundingRect ().topLeft ()),
                   mapToScene (boundingRect ().bottomRight ()));
}
//------------------------------------
QRectF ImagePart::clientRectToScene ()
{
    return QRectF (mapToScene (rect ().topLeft ()),
                   mapToScene (rect ().bottomRight ()));
}
//-----------------------------------------------------------------
void ImagePart::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if (role () == 2) {
        event->accept ();
        m_diff = event->pos ();
    }
    else {
        QGraphicsWidget::mousePressEvent (event);
    }
}
//--------------------------------------------------------------
void ImagePart::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    if (role () == 2) {
        event->accept ();
        setPos (event->scenePos () - m_diff);
    }
    else {
        QGraphicsWidget::mouseMoveEvent (event);
    }
}
//--------------------------------------------------------------------
void ImagePart::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    if (role () == 2) {
        event->accept ();
        m_diff = QPointF (0,0);
    }
    else {
        QGraphicsWidget::mouseReleaseEvent (event);
    }

}
#include <QDebug>
void ImagePart::hoverEnterEvent(QGraphicsSceneHoverEvent */*event*/)
{
}
//---------------------------------------------------------------
void ImagePart::paint (QPainter *painter,
                       const QStyleOptionGraphicsItem */*option*/,
                       QWidget* /*widget*/)
{
    painter->save ();
    painter->setRenderHints (QPainter::TextAntialiasing);
    const QRectF& rectToPaint (rect ());
    painter->setClipping (true);
    painter->setClipRect (rectToPaint);

    // surface
    // 1- pen
    QPen pen (ipColor ().darker (150));
    pen.setCosmetic (true);
    pen.setWidthF (3.0);
    painter->setPen (pen);
    //    painter->setPen (Qt::NoPen);

    // 2- brush
    QColor color (ipColor ());
    color.setAlpha (isSelected () ? 100 : 50);
    if (role () > 1) color.setAlpha (10);
    painter->setBrush (QBrush (color));

    // dessin
    painter->drawRect (rectToPaint);

    // numéro
    if (isGreen ())
    {
        QPen pen (Qt::gray);
        pen.setCosmetic (true);
        pen.setCapStyle (Qt::RoundCap);
        painter->setPen (pen);
        painter->setOpacity (0.7);
        QFont font ("Serif");
        font.setStyleStrategy (QFont::ForceOutline);
        font.setWeight (QFont::Black);
        font.setPixelSize (2 * rect ().height () / 3);
        painter->setFont (font);
        painter->drawText (rect (),
                           Qt::AlignCenter,
                           QString ("%1").arg (order ()));
    }
    painter->restore ();
}
//--------------------------------------------------------------------------------------------------------------------
void ImagePart::paintWindowFrame (QPainter *painter, const QStyleOptionGraphicsItem */*style*/, QWidget */*widget*/)
{
    painter->save ();
    painter->setRenderHints (QPainter::TextAntialiasing);
    const QRectF& rectToPaint (boundingRect ());
    painter->setClipping (true);
    painter->setClipRect (rectToPaint);

    // 1- pen
    QColor penColor (Qt::gray);
    penColor.setAlpha (64);
    QPen pen (penColor);
    pen.setCosmetic (true);
    pen.setWidthF (borderSize ());
    painter->setPen (pen);

    // 2- pas de brush
    painter->setBrush (Qt::NoBrush);

    // dessin
    painter->drawRect (rectToPaint.adjusted (borderSize () / 2.0,
                                             borderSize () / 2.0,
                                             -borderSize () / 2.0,
                                             -borderSize () / 2.0));
    painter->restore ();
}
//-----------------------------------------------------------------------
QVector<QRectF> ImagePart::partsToRects (const QList<ImagePart *> &list)
{
    const int size = list.size ();
    QVector<QRectF> result (size);

    for (int index = 0 ; index != size ; ++index) {
        result[index] = list.at (index)->rectangle ();
    }
    return result;
}
//-----------------------------------------------------------------------------
//QList<SelectRect> ImagePart::partsToSelectRects (const QList<ImagePart *> &list)
//{
//    QList<SelectRect> result;

//    foreach (ImagePart* part, list) {
//        result << part->toSelectRect ();
//    }
//    return result;
//}
